package com.larry.design.creatation.singleton;

/**
 * @author larry
 * @Title: design-frame
 * @Package com.larry.design.creatation.singleton
 * @Description: 单例模式
 * @date 8/31/2112:00 PM
 */
public class Person {
    private String name;
    private String age;

    //懒汉,饿汉
    private volatile static Person instance;
    //private final static Person instance = new Person();饿汉

    //构造器私有,外部不能实例化
    private Person(){
        System.out.println("创建了...");
    }


    //提供给外部的方法

    /**
     * 1.public static synchronized Person getPerson()锁太大
     * 2.双重检测 + 内存可见性
     * if (instance == null){
     *             synchronized (Person.class){
     *                 if (instance == null){
     *                     Person person = new Person();
     *                     instance = person;
     *                 }
     *             }
     *
     *         }
     * @return
     */
    public static Person getPerson(){
        //如果没有再去创建
        if (instance == null){
            synchronized (Person.class){
                if (instance == null){
                    Person person = new Person();
                    instance = person;
                }
            }

        }
        return instance;
    }
}
